﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using DTO;
using BUS;
using System.Collections.Generic;
using System.Xml;

namespace WebRaoVat
{
    public partial class DangKyNhanTinRaoVat : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Session["TrangHienTai"] = "DangKyNhanTinRaoVat.aspx";

            if (!IsPostBack)
            {
                //ddlChuyenMuc
                ddlChuyenMuc.Items.Add("Tất cả");

                List<DanhMucConDTO> DSDanhMucCon = DanhMucConBUS.getAll();

                for (int i = 0; i < DSDanhMucCon.Count; ++i)
                {
                    if (i > 4)
                    {
                        ddlChuyenMuc.Items.Add(DSDanhMucCon[i].TenDanhMucCon);
                    }
                }

                ddlChuyenMuc.SelectedIndex = 0;
            }
        }

        protected void Page_PreInit(object sender, EventArgs e)
        {
            Page.Theme = ((XmlElement)XL_XML.ReadNodeSingle(XL_XML.ReadDocumentFile(Server.MapPath("Gui.xml")),
            "/GIAODIEN/THEME")).GetAttribute("ten");
        }

        public List<string> ListMessage = new List<string>();

        protected void btnChapNhan_Click(object sender, EventArgs e)
        {
            bool bDKThanhCong = false;
            ListMessage.Clear();

            string strEmail = txtEmail.Text.Trim();

            if (ddlChuyenMuc.SelectedValue == "Tất cả")
            {
                if (EmailDangKyNhanTinRaoVatBUS.KiemTraDaDangKyTatCa(strEmail) == true)
                {
                    ListMessage.Add("* Bạn đã đăng ký nhận từ tất cả chuyên mục rồi");
                    return;
                }

                List<DanhMucConDTO> DSDanhMucCon = DanhMucConBUS.getAll();

                for (int i = 0; i < DSDanhMucCon.Count; ++i)
                {
                    try
                    {
                        EmailDangKyNhanTinRaoVatDTO emaildk = new EmailDangKyNhanTinRaoVatDTO();

                        emaildk.EmailNhanTinRaoVat = strEmail;
                        emaildk.MaDanhMucCon = DSDanhMucCon[i].MaDanhMucCon;
                        emaildk.ThoiGianDangKy = DateTime.Now;
                        emaildk.Deleted = 0;

                        if (EmailDangKyNhanTinRaoVatBUS.KiemTraDangKyRoi(emaildk.EmailNhanTinRaoVat, emaildk.MaDanhMucCon) == false)
                        {
                            EmailDangKyNhanTinRaoVatBUS.Insert(emaildk);
                            bDKThanhCong = true;
                        }
                    }
                    catch (System.Exception ex)
                    {
                        bDKThanhCong = false;
                    }
                }
            }
            else
            {
                int maDanhMucCon = DanhMucConBUS.getMa(ddlChuyenMuc.SelectedValue);

                try
                {
                    EmailDangKyNhanTinRaoVatDTO emaildk = new EmailDangKyNhanTinRaoVatDTO();

                    emaildk.EmailNhanTinRaoVat = strEmail;
                    emaildk.MaDanhMucCon = maDanhMucCon;
                    emaildk.ThoiGianDangKy = DateTime.Now;
                    emaildk.Deleted = 0;

                    if (EmailDangKyNhanTinRaoVatBUS.KiemTraDangKyRoi(emaildk.EmailNhanTinRaoVat, emaildk.MaDanhMucCon) == false)
                    {
                        EmailDangKyNhanTinRaoVatBUS.Insert(emaildk);
                        bDKThanhCong = true;
                    }
                    else
                    {
                        ListMessage.Add("* Bạn đã đăng ký nhận từ chuyên mục này rồi");
                        return;
                    }
                }
                catch (System.Exception ex)
                {
                    bDKThanhCong = false;
                }
            }

            if (bDKThanhCong == true)
            {
                ListMessage.Add("Đăng ký thành công !");
            }
            else
            {
                ListMessage.Add("Đăng ký không thành công !");
            }
        }
    }
}
